#! /bin/sh
. "${srcdir=.}/init.sh"; path_prepend_ . ../src

# Test recognition of JavaScript format strings.

cat <<\EOF > f-js-1.data
# Valid: no argument
"abc%%"
# Valid: one character argument
"abc%c"
# Valid: one string argument
"abc%s"
# Valid: one integer argument
"abc%b"
# Valid: one integer argument
"abc%d"
# Valid: one integer argument
"abc%o"
# Valid: one integer argument
"abc%x"
# Valid: one integer argument
"abc%X"
# Valid: one floating-point argument
"abc%f"
# Valid: one object argument
"abc%j"
# Valid: one argument with flags
"abc%Id"
# Valid: one argument with width
"abc%2d"
# Valid: one argument with precision
"abc%.4f"
# Valid: one argument with width and precision
"abc%14.4f"
# Invalid: unterminated
"abc%"
# Invalid: unknown format specifier
"abc%y"
# Invalid: flags after width
"abc%1Ig"
# Invalid: twice precision
"abc%.4.2f"
# Valid: three arguments
"abc%d%j%j"
EOF

: ${XGETTEXT=xgettext}
n=0
while read comment; do
  read string
  n=`expr $n + 1`
  cat <<EOF > f-js-1-$n.in
gettext(${string});
EOF
  # Hide xgettext's "The translator cannot reorder the arguments." warnings.
  ${XGETTEXT} -L JavaScript -o f-js-1-$n.po f-js-1-$n.in 2> f-js-1.err \
    || { cat f-js-1.err 1>&2; exit 1; }
  test -f f-js-1-$n.po || exit 1
  fail=
  if echo "$comment" | grep 'Valid:' > /dev/null; then
    if grep javascript-format f-js-1-$n.po > /dev/null; then
      :
    else
      fail=yes
    fi
  else
    if grep javascript-format f-js-1-$n.po > /dev/null; then
      fail=yes
    else
      :
    fi
  fi
  if test -n "$fail"; then
    echo "Format string recognition error:" 1>&2
    cat f-js-1-$n.in 1>&2
    echo "Got:" 1>&2
    cat f-js-1-$n.po 1>&2
    exit 1
  fi
  rm -f f-js-1-$n.in f-js-1-$n.po
done < f-js-1.data

exit 0
